package com.liu.common.aspect;

import com.liu.common.content.RedisKeyInterface;
import com.liu.common.exception.WarframeBotException;
import com.liu.common.interfases.CheckLoginInterface;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Slf4j
@Aspect
@Component
public class CheckLoginAspect {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Before("@annotation(checkLoginInterface)")
    public void before(JoinPoint joinPoint, CheckLoginInterface checkLoginInterface) throws WarframeBotException {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String groupId  = sra.getRequest().getHeader("groupId");
        String userId  = sra.getRequest().getHeader("userId");
        String loginStatus = redisTemplate.opsForValue().get(RedisKeyInterface.USER_LOGIN_WEB_REDIS_KEY + groupId + "" + userId);
        if (loginStatus == null) {
            throw new WarframeBotException(999, "请先登录");
        }
        if (loginStatus.equals("0")) {
            throw new WarframeBotException(999, "请先再QQ私聊中回复相关信息");
        }
    }

}
